<template>
	<view>
		<template v-if="!loading">
			<view>
				<view class="u-text-center u-p-t-20">
					<image :src="pic" mode="widthFix"></image>
				</view>

				<view class="u-p-20">
					<!-- 商品基本信息 -->
					<view class="base-info">
						<text class="title u-font-40">{{title}}</text>
						<view class="u-flex u-row-between u-m-t-20 u-m-b-20">
							<text class="price u-font-32">¥{{price}}</text>
							<text class="small">库存：{{stock}}</text>
							<text class="small">销量：{{sales}}</text>
						</view>
					</view>
				</view>
				<view class="u-flex u-flex-center u-m-b-20">
					<u-tabs :list="tabList" @click="tabChange"></u-tabs>
				</view>
				<view style="padding-bottom: 75px;">
					<!-- 商品详情页面 -->
					<template v-if="current == 0">
						<view class="u-p-10">
							<u-parse :content="content"></u-parse>
						</view>
					</template>
					<!-- 商品评论页面 -->
					<template v-if="current == 1">
						<template v-if="commentList.length">
							<view class="comment" v-for="(res, index) in commentList" :key="index">
								<view class="left">
									<image :src="res.user.avatar_url" mode="aspectFill"></image>
								</view>
								<view class="right">
									<view class="top">
										<view class="name">{{ res.user.name }}</view>
										<view class="like">
											<view class="num">{{ res.star }}</view>
											<u-icon name="thumb-up" :size="30" color="#9a9a9a"></u-icon>
										</view>
									</view>
									<view class="content">{{ res.content }}</view>
								</view>
							</view>
						</template>
						<template v-else>
							<u-empty text="暂无评论" icon="http://cdn.uviewui.com/uview/empty/comment.png">
							</u-empty>
						</template>
					</template>
					<!-- 推荐商品页面 -->
					<template v-if="current == 2">
						<template v-if="like_goods.length">
							<u-row class="u-flex-wrap u-p-l-8 u-p-r-8">
								<u-col span="6" v-for="(item,index) in like_goods" :key="index">
									<goodsCard :item="item"></goodsCard>
								</u-col>
							</u-row>
						</template>
						<template v-else>
							<u-empty text="暂无推荐商品" mode="list">
							</u-empty>
						</template>
					</template>
				</view>
			</view>
			<view class="navigation">
				<view class="left">
					<view class="item" @click='toCollect'>
						<u-icon v-if="!collect" name="heart" :size="40" color="#606266"></u-icon>
						<u-icon v-else name="heart-fill" :size="40" color="#606266"></u-icon>
						<view class="text u-line-1">收藏</view>
					</view>
					<view class="item" @click="toCart">
						<u-icon name="shopping-cart" :size="40" color="#606266"></u-icon>
						<u-badge numberType="overflow" max='99' :absolute="true" shape="horn" :offset="[-1,-9]"
							type="error" :value="cartNum"></u-badge>
						<view class="text u-line-1">购物车</view>
					</view>
				</view>
				<view class="right">
					<u-button @click="addCart" :disabled="stock == 0 ? true:false" :customStyle="cs" class="buy btn u-line-1">加入购物车
					</u-button>
					<!-- <view class="buy btn u-line-1">加入购物车</view> -->
				</view>
			</view>
		</template>
		<template v-else>
			<view class="loading">
				<u-loading-icon :vertical="true" text="拼命加载中" color="red"></u-loading-icon>
			</view>
		</template>
	</view>
</template>

<script>
	let goodsID, access_token;
	export default {
		data() {
			return {
				cs:{
					'background-color': "#ff7900",
					'line-height': "66rpx",
					padding: "0 30rpx",
					'border-radius': "36rpx",
					color: "#ffffff",
					width: "79%",
					'text-align': "center",
					overflow: "hidden",
					'white-space': "nowrap",
					'text-overflow': "ellipsis"
				},
				cartNum: 0,
				commentList: [],
				content: '',
				current: 0, //tabs标签当前处于哪一个位置
				loading: true,
				pic: '',
				title: '',
				price: 0,
				stock: 0,
				sales: 0,
				tabList: [{
						name: '商品详情'
					},
					{
						name: '商品评论',
						badge: {
							value: 0
						}
					},
					{
						name: '推荐商品'
					}
				],
				like_goods: [],
				collect: 0
			}
		},
		onLoad(options) {
			//console.log(options);
			access_token = uni.getStorageSync('access_token');
			goodsID = options.id;
			this.getData(goodsID);	
			this.cartLen();
		},
		methods: {
			toCart() {
				uni.$u.route({
					type: 'tab',
					url: '/pages/cart/cart'
				})
			},
			async addCart() {
				let res = await uni.$u.http.post('/api/carts', {
					goods_id: goodsID
				}, {
					header: {
						Authorization: 'bearer ' + access_token
					}
				})
				if (res === false) {
					setTimeout(() => {
						uni.$u.toast('添加购物车失败');
					}, 1500)
				} else {
					uni.$u.toast('添加购物车成功');
					this.cartNum++;
				}
			},
			async cartLen() {
				//先判断当前是否处于登录状态
				if (!uni.$u.utils.isLogin()) {
					return;
				}
				const res = await uni.$u.http.get('/api/carts', {
					params: {
						include: 'goods'
					},
					header: {
						Authorization: 'bearer ' + access_token
					}
				})
				console.log(res);
				//返回的结果是一个数组。每一个元素代表一个商品，元素里面的num字段表示这一个商品的具体数量。购物车总量就是所有元素是num之和。
				/* this.cartNum = res.data.reduce((pre,item)=>{
					return parseInt(pre) + parseInt(item.num);
				},0) */
				let num = 0;
				for (let i = 0; i < res.data.length; i++) {
					num = num + parseInt(res.data[i].num);
				}
				this.cartNum = num;
			},
			async toCollect() {
				//先判断当前是否处于登录状态
				if (!uni.$u.utils.isLogin()) {
					return;
				}
				//调用接口，实现添加或取消收藏
				let res = await uni.$u.http.post('/api/collects/goods/' + goodsID, {}, {
					header: {
						Authorization: 'bearer ' + access_token
					}
				})
				if (this.collect) {
					uni.$u.toast('取消收藏成功');
				} else {
					uni.$u.toast('收藏成功');
				}
				this.collect = !this.collect;
			},
			tabChange(e) {
				console.log(e);
				this.current = e.index;
			},
			async getData(id) {
				let res = await uni.$u.http.get(`/api/goods/${id}`,{
					header: {
						Authorization: 'bearer ' + access_token
					}
				});
				//接下来填充数据
				this.pic = res.goods.cover_url;
				this.title = res.goods.title;
				this.price = res.goods.price;
				this.stock = res.goods.stock;
				this.sales = res.goods.sales;
				this.tabList[1].badge.value = res.goods.comments.length;
				this.content = res.goods.details;
				this.commentList = res.goods.comments;
				this.like_goods = res.like_goods;
				this.collect = res.goods.is_collect;
				//填充好数据，把loading状态改变一下
				this.loading = false;
				console.log(res);
			}

		}
	}
</script>

<style scoped lang="scss">
	/* #ifdef MP-WEIXIN */
	/deep/ .u-button--square{
		border-radius: 30px !important;
	}
	/deep/ .u-button--info{
		background-color: #007AFF;
	}
	/* #endif */
	.loading {
		text-align: center;
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		/* 元素会以自身的左上角为原点，往上往左分别移动自身长宽的50%，以使其居于中心位置。 */
	}

	.title,
	.price {
		font-weight: bolder;
	}

	.price {
		color: rgb(209, 45, 58);
	}

	.small {
		font-size: 24rpx;
		color: #ccc;
	}

	.comment {
		display: flex;
		padding: 30rpx;

		.left {
			image {
				width: 64rpx;
				height: 64rpx;
				border-radius: 50%;
				background-color: #f2f2f2;
			}
		}

		.right {
			flex: 1;
			padding-left: 20rpx;
			font-size: 30rpx;

			.top {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 10rpx;

				.name {
					color: #5677fc;
				}

				.like {
					display: flex;
					align-items: center;
					color: #9a9a9a;
					font-size: 26rpx;

					.num {
						margin-right: 4rpx;
						color: #9a9a9a;
					}
				}

				.highlight {
					color: #5677fc;

					.num {
						color: #5677fc;
					}
				}
			}

			.content {
				margin-bottom: 10rpx;
			}

			.reply-box {
				background-color: rgb(242, 242, 242);
				border-radius: 12rpx;

				.item {
					padding: 20rpx;
					border-bottom: solid 2rpx $u-border-color;

					.username {
						font-size: 24rpx;
						color: #999999;
					}
				}

				.all-reply {
					padding: 20rpx;
					display: flex;
					color: #5677fc;
					align-items: center;

					.more {
						margin-left: 6rpx;
					}
				}
			}

			.bottom {
				margin-top: 20rpx;
				display: flex;
				font-size: 24rpx;
				color: #9a9a9a;

				.reply {
					color: #5677fc;
					margin-left: 10rpx;
				}
			}
		}
	}

	.navigation {
		display: flex;
		border: solid 2rpx #f2f2f2;
		background-color: #ffffff;
		padding: 16rpx 0;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 99999;
		width: 100%;
		justify-content: space-between;

		.left {
			flex: 5;
			display: flex;
			font-size: 20rpx;
			justify-content: space-around;

			.item {
				position: relative;
				text-align: center;
			}
		}

		.right {
			flex: 7;
			display: flex;
			font-size: 28rpx;
			align-items: center;

			.btn {
				line-height: 66rpx;
				padding: 0 30rpx;
				border-radius: 36rpx;
				color: #ffffff;
				width: 79%;
				text-align: center;
			}

			.cart {
				background-color: #ed3f14;
				margin-right: 30rpx;
			}

			
		}
	}
	.buy {
		background-color: #ff7900;
	}
</style>
